package com.zfjt.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.zfjt.constants.InterfaceDefinition;
import com.zfjt.entity.FhNewsBean;
import com.zfjt.entity.NewsBean;
import com.zfjt.entity.ReturnBean;
import com.zfjt.service.NewsBeanService;

/**
 * 新闻列表接口
 * @author xk
 * @version 1.0
 * @date 2017年3月29日
 */
@Controller
public class NewsListController {
	
	//返回对象
	private ReturnBean mReturn;
	
	private Map<String, String> fhdata;
		
	@Autowired
	private NewsBeanService mNews;
	
	@RequestMapping(value="00033")
	@ResponseBody
	public ReturnBean Brabch(HttpSession hseesion){
		mReturn = new ReturnBean();
		fhdata = new HashMap<>();
		//传过来的参数
		String data =  (String) hseesion.getAttribute("data");
		if (data.equals(InterfaceDefinition.IStatusCode.TOKEN_FAILURE)) {
			mReturn.setPortnum("00033");
			mReturn.setCode("001");
			fhdata.put("error", "TOKEN_ERROR");
			mReturn.setData(JSON.toJSONString(fhdata));
		}else{
			List<NewsBean> mData = mNews.selectAll();
			List<FhNewsBean> fh = new ArrayList<>();
			for (int i = 0; i < mData.size(); i++) {
				FhNewsBean dg = new FhNewsBean(mData.get(i).getId() +"", mData.get(i).getTitle(), mData.get(i).getAddTime());
				fh.add(dg);
			}
			mReturn.setPortnum("00033");
			mReturn.setCode("000");
			mReturn.setData(JSON.toJSONString(fh));
		}
		return mReturn;
	}
}
